package com.fly.demo;

import java.util.List;
import java.util.Scanner;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;

import com.fly.core.config.MybatisConfiguration;
import com.fly.core.config.spring.ApplicationConfiguration;
import com.fly.demo.dao.UserInfoMapper;
import com.fly.demo.model.UserInfo;
import com.fly.demo.model.UserInfoExample;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * Transactional配置有问题待排查
 * 
 * @author 00fly
 * @version [版本号, 2021年3月28日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Slf4j
@Transactional
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {ApplicationConfiguration.class, MybatisConfiguration.class})
public class MyTestClass
{
    @Autowired
    UserInfoMapper userInfoMapper;
    
    /**
     * 演示UserInfoMapper定义在UserInfoMapper.xml、UserInfoMapper2.xml
     * 
     * @see [类、类#方法、类#成员]
     */
    @Test
    public void test()
    {
        try (Scanner sc = new Scanner(System.in))
        {
            do
            {
                List<UserInfo> users = userInfoMapper.selectByExample(new UserInfoExample());
                for (UserInfo user : users)
                {
                    log.info("user => {}", ReflectionToStringBuilder.toString(user, ToStringStyle.JSON_STYLE));
                    userInfoMapper.deleteById(user.getId());
                    
                    user.setEmail(DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
                    userInfoMapper.insert(user);
                    log.info("user => {}", ReflectionToStringBuilder.toString(user, ToStringStyle.JSON_STYLE));
                }
                
                log.info("------------输入x退出,回车换行继续------------");
            } while (!StringUtils.equalsIgnoreCase(sc.nextLine(), "x"));
            log.info("------------成功退出------------");
        }
    }
    
}
